package cn.marinda.spritefragment;

import cn.lovelycatv.custom.CustomItemObject;
import cn.lovelycatv.custom.listener.ContainerEvent;
import cn.marinda.spritefragment.commands.Command;
import cn.marinda.spritefragment.data.*;
import cn.marinda.spritefragment.listener.PlayerListener;
import net.milkbowl.vault.economy.Economy;
import org.bukkit.Bukkit;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.java.JavaPlugin;

import java.util.Map;

public final class SpriteFragment extends JavaPlugin {
    public static SpriteFragment plugin;
    private ConfigManager gm = null;
    public static Economy eco = null;
    public static boolean isVaultEnabled = false;
    @Override
    public void onEnable() {
        plugin = this;
        saveDefaultConfig();
        saveResource("message.yml",false);
        saveResource("items.yml",false);
        saveResource("exchange.yml",false);
        gm = new ConfigManager();
        if (Bukkit.getPluginManager().isPluginEnabled("Vault")) {
            Bukkit.getServer().getConsoleSender().sendMessage("§e已找到Vault");
            if (setupEconomy()) {
                Bukkit.getServer().getConsoleSender().sendMessage("§aVault初始化成功");
                isVaultEnabled = true;
            }else {
                Bukkit.getServer().getConsoleSender().sendMessage("§cVault初始化失败");
                isVaultEnabled = false;
            }
        }else {
            Bukkit.getServer().getConsoleSender().sendMessage("§c未找到Vault");
            isVaultEnabled = false;
        }
        // Plugin startup logic
        Bukkit.getPluginManager().registerEvents(new PlayerListener(),this);
        Bukkit.getPluginManager().registerEvents(new ContainerEvent(),this);
        Bukkit.getPluginCommand("ss").setExecutor(new Command());
        Bukkit.getServer().getConsoleSender().sendMessage("§a§l[SpriteSynthesis]插件加载成功！");
        
    }

    @Override
    public void onDisable() {
        Bukkit.getServer().getConsoleSender().sendMessage("§a§l[SpriteSynthesis]插件卸载成功！");
        // Plugin shutdown logic
    }

    public static Economy getEco() {
        return eco;
    }

    private boolean setupEconomy()
    {
        RegisteredServiceProvider<Economy> economyProvider = getServer().getServicesManager().getRegistration(net.milkbowl.vault.economy.Economy.class);
        if (economyProvider != null) {
            eco = economyProvider.getProvider();
        }
        return (eco != null);
    }
}
